package com.aier.user.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.user.entity.AppraiseComment;

public class AppraiseCommentRowMapper implements RowMapper<AppraiseComment> {

    @SuppressWarnings("unused")
    private int type;

    public AppraiseCommentRowMapper() {
    }

    public AppraiseCommentRowMapper(int type) {
        this.type = type;
    }

    @Override
    public AppraiseComment mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseHome(new AppraiseComment(), rs);
    }

    public static AppraiseComment parseHome(AppraiseComment h, ResultSet rs) throws SQLException {
        return parseHome(h, rs, null);
    }

    public static AppraiseComment parseHome(AppraiseComment h, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            h.setApcid(rs.getLong(su+"apcid"));
        } catch (SQLException e) {
            h.setApcid(null);
        }
        try {
            h.setApid(rs.getLong(su+"apid"));
        } catch (SQLException e) {
            h.setApid(null);
        }
        try {
            h.setCuid(rs.getLong(su+"cuid"));
        } catch (SQLException e) {
            h.setCuid(null);
        }
        try {
            h.setCname(rs.getString(su+"cname"));
        } catch (SQLException e) {
            h.setCname(null);
        }
        try {
            String str = rs.getString(su+"content");
            if (str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            h.setContent(str);
        } catch (SQLException e) {
            h.setContent(null);
        }
        try {
            h.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            h.setCdate(null);
        }
        try {
            h.setHeadimg(rs.getString(su+"headimg"));
        } catch (SQLException e) {
            h.setHeadimg(null);
        }
        return h;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, AppraiseComment h) {
        par.put("apcid",h.getApcid());
        par.put("apid",h.getApid());
        par.put("cuid",h.getCuid());
        par.put("cname", h.getCname());
        par.put("content",h.getContent());
        par.put("cdate",h.getCdate());
        par.put("headimg",h.getHeadimg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, AppraiseComment h) {
        args.put("apcid",h.getApcid() );
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final AppraiseComment h) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(h),
                        new String[] { "apcid" });
                parsePS(ps, h);
                return ps;
            }

        };
    }

    private static String parseSQL(AppraiseComment h) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into appraise_comment(");
        int count = 0;
        if (null != h.getApid()) {
            sql.append("apid,");
            count++;
        }
        if (null != h.getCuid()) {
            sql.append("cuid,");
            count++;
        }
        if (null != h.getCname()) {
            sql.append("cname,");
            count++;
        }
        if (null != h.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != h.getCdate()) {
            sql.append("cdate,");
            count++;
        }
        
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, AppraiseComment h)
        throws SQLException {
        int count = 1;
        if (null != h.getApid()) {
            ps.setLong(count++,h.getApid());
        }
        if (null != h.getCuid()) {
            ps.setLong(count++,h.getCuid());
        }
        if (null != h.getCname()) {
            ps.setString(count++,h.getCname());
        }
        if (null != h.getContent()) {
            ps.setString(count++,h.getContent() );
        }
        if (null != h.getCdate()) {
            ps.setTimestamp(count++,h.getCdate());
        }
    }

}
